# Maintainer: Alexey Pavlov <alexpux@gmail.com>

_realname=setuptools

pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgname=("${MINGW_PACKAGE_PREFIX}-python2-setuptools" "${MINGW_PACKAGE_PREFIX}-python3-setuptools")
_py3_base=3.4
pkgver=5.2
pkgrel=1
pkgdesc="Easily download, build, install, upgrade, and uninstall Python packages (mingw-w64)"
arch=('any')
license=('PSF')
url="http://pypi.python.org/pypi/setuptools"
depends=("${MINGW_PACKAGE_PREFIX}-python2" "${MINGW_PACKAGE_PREFIX}-python3")
source=(http://pypi.python.org/packages/source/s/setuptools/${_realname}-${pkgver}.tar.gz
        'mingw-python-fix.patch')
md5sums=('ea6ed9ab1a4abe978dade73a592a229c'
         'c141e15510d51e35b1d1578b35510f9a')
 
prepare() {
   cd "${srcdir}/setuptools-${pkgver}"
   patch -p1 -i ${srcdir}/mingw-python-fix.patch

   cd "${srcdir}"
 
   cp -r setuptools-${pkgver} setuptools-python2
   cp -r setuptools-${pkgver} setuptools-python3
 
   cd "${srcdir}"/setuptools-python2
   sed -i -e "s|^#\!.*/usr/bin/python|#!/usr/bin/python3|" setuptools/tests/test_resources.py
   sed -i -e "s|^#\!.*/usr/bin/env python|#!/usr/bin/env python3|" setuptools/command/easy_install.py
 
   cd "${srcdir}"/setuptools-python3
   sed -i -e "s|^#\!.*/usr/bin/python|#!/usr/bin/python2|" setuptools/tests/test_resources.py
   sed -i -e "s|^#\!.*/usr/bin/env python|#!/usr/bin/env python2|" setuptools/command/easy_install.py
}

build() {
  # Build python 3 module
  cd "${srcdir}"/setuptools-python3
  ${MINGW_PREFIX}/bin/python3 setup.py build

  # Build python 2 module
  cd "${srcdir}"/setuptools-python2
  ${MINGW_PREFIX}/bin/python2 setup.py build
}

package_python3-setuptools() {
  depends=("${MINGW_PACKAGE_PREFIX}-python3>=3.3")

  pushd ${MINGW_PREFIX} > /dev/null
  local _mingw_prefix=`pwd -W`
  popd > /dev/null

  cd "${srcdir}/setuptools-python3"
  MSYS2_ARG_CONV_EXCL="--prefix=;--install-scripts=;--install-platlib=" \
    ${MINGW_PREFIX}/bin/python3 setup.py install --prefix=${MINGW_PREFIX#\/} --root="${pkgdir}" --optimize=1 --skip-build
    
  # Remove files conflicted with python2-setuptools
  #rm -f ${pkgdir}${MINGW_PREFIX}/bin/easy_install{-script.py,.exe,.exe.manifest}
  mv ${pkgdir}${MINGW_PREFIX}/bin/easy_install-script.py ${pkgdir}${MINGW_PREFIX}/bin/easy_install-${_py3_base}-script.py
  mv ${pkgdir}${MINGW_PREFIX}/bin/easy_install.exe ${pkgdir}${MINGW_PREFIX}/bin/easy_install-${_py3_base}.exe
  [[ -f ${pkgdir}${MINGW_PREFIX}/bin/easy_install.exe.manifest ]] && {
    mv ${pkgdir}${MINGW_PREFIX}/bin/easy_install.exe.manifest ${pkgdir}${MINGW_PREFIX}/bin/easy_install-${_py3_base}.exe.manifest
    sed -e "s|easy_install|easy_install-${_py3_base}|g" -i ${pkgdir}${MINGW_PREFIX}/bin/easy_install-${_py3_base}.exe.manifest
  }
  sed -e "s|${_mingw_prefix}|${MINGW_PREFIX}|g" -i ${pkgdir}${MINGW_PREFIX}/bin/easy_install-${_py3_base}-script.py
}
 
package_python2-setuptools() {
  depends=("${MINGW_PACKAGE_PREFIX}-python2>=2.7")
  
  pushd ${MINGW_PREFIX} > /dev/null
  local _mingw_prefix=`pwd -W`
  popd > /dev/null
  
  cd "${srcdir}/setuptools-python2"
  MSYS2_ARG_CONV_EXCL="--prefix=;--install-scripts=;--install-platlib=" \
    ${MINGW_PREFIX}/bin/python2 setup.py install --prefix=${MINGW_PREFIX#\/} --root="${pkgdir}" --optimize=1 --skip-build
  #rm "${pkgdir}${MINGW_PREFIX}/bin/easy_install"
  sed -e "s|${_mingw_prefix}|${MINGW_PREFIX}|g" -i ${pkgdir}${MINGW_PREFIX}/bin/easy_install-script.py
}

package_mingw-w64-i686-python2-setuptools() {
  install=${_realname}2-${CARCH}.install
  package_python2-setuptools
}

package_mingw-w64-i686-python3-setuptools() {
  install=${_realname}3-${CARCH}.install
  package_python3-setuptools
}

package_mingw-w64-x86_64-python2-setuptools() {
  install=${_realname}2-${CARCH}.install
  package_python2-setuptools
}

package_mingw-w64-x86_64-python3-setuptools() {
  install=${_realname}3-${CARCH}.install
  package_python3-setuptools
}
